import {Component, ViewChild} from '@angular/core';
import {IonicPage, MenuController, AlertController, Content} from 'ionic-angular';
import {OrderProvider} from '../../../providers/orderProvider';
import {OrderNumber, } from '../../../models/OrderNumber';
import {Order} from "../../../models/Order";

import {KitProvider} from "../../../core/foundations/KitProvider";
import {CustomerPage} from "../../../core/foundations/CustomerPage";

/**
 * Generated class for the IndentPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-indent',
  templateUrl: 'indent.html',
})
export class IndentPage extends CustomerPage {
  @ViewChild(Content) content: Content;
  orders: { OrderNumber: OrderNumber, Orders: Order[], delete?: boolean }[];
  noMore = false;
  nextPage = 1;
  storeID = "all";
  tableID = "all";

  constructor(public menu: MenuController,
              public alert: AlertController,
              public orderPro: OrderProvider,
              public kit: KitProvider,) {
    super();
    this.checkLogin();
  }

  ionViewDidLoad() {

    console.log('ionViewDidLoad IndentPage');
    let loading = this.kit.loadingPresent();

    this.orderPro.getOrders().subscribe(res => {
      loading.dismiss();
      res = res.data;

      if (this.nextPage == res.last_page || res.last_page == 0)
        this.noMore = true;
      else
        this.nextPage = this.nextPage + 1;


      this.orders = res.data;
      console.log('indent', this.orders);
      this.content.resize();
    });
    this.getOrder();

  }

  /*payment(d) {
    this.orderPro.cancel(d.OrderNumber).subscribe(res => {
      if (res.success) {
        d.OrderNumber.Order_State = OrderState.Order_Paid;
        this.content.resize();
      }
    });
  }

  cancel(d) {
    this.orderPro.cancel(d.OrderNumber).subscribe(res => {
      if (res.success) {
        d.OrderNumber.Order_State = OrderState.Order_Paid;
        this.content.resize();

      }
    });

  }

  delete(d) {

    this.orderPro.delete(d.OrderNumber).subscribe(res => {
      if (res.success) {
        d.delete = true;
        console.log(this.orders);
        this.content.resize();

      }
    });

  }
*/
  getOrder() {

    let loading = this.kit.loadingPresent();
    let store_id, table_id;
    if (this.storeID != 'all')
      store_id = this.storeID;
    if (this.tableID != 'all')
      table_id = this.tableID;
    this.orderPro.getOrders(1, store_id, table_id).subscribe(res => {
      loading.dismiss();
      res = res.data;
      if (this.nextPage == res.last_page || res.last_page == 0)
        this.noMore = true;
      else
        this.nextPage = this.nextPage + 1;
      this.orders = res.data;
      console.log('indent', this.orders);
      this.content.resize();
    });
  }

  getMore(event) {
    if (!this.noMore) {
      let loading = this.kit.loadingPresent();
      this.orderPro.getOrders(this.nextPage).subscribe(res => {
        loading.dismiss();
        res = res.data;
        if (this.nextPage == res.last_page)
          this.noMore = true;

        else
          this.nextPage = this.nextPage + 1;
        for (let o of res.data) {
          this.orders.push(o);
        }
        event.complete();
      });
    } else
      event.complete();
  }
}
